package hesso.smartdraw.ch;

import java.util.ArrayList;

import hesso.smartdraw.ch.db.DBHelper;
import hesso.smartdraw.ch.model.Device;
import hesso.smartdraw.ch.model.Input;
import hesso.smartdraw.ch.model.Installed;
import hesso.smartdraw.ch.model.Output;
import hesso.smartdraw.ch.model.Room;
import hesso.smartdraw.ch.model.Wall;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class EditDeviceScreen extends Activity {
	
	private Context context = this;
	
	private Installed installedDevice;
	
	private int installedId;
	
	private Installed installed;
	
	private int roomId;
	
	private int level;
	private int buildingId;
	
	private int currentWallIndex;
	
	private ArrayList<Device> devices = new ArrayList<Device>();
	
	private ArrayList<Input> inputs = new ArrayList<Input>();
	
	private ArrayList<Output> outputs = new ArrayList<Output>();
	
	private String[] deviceTypeArray;
	private String[] deviceInputsArray;
	private String[] deviceOutputsArray;
	
	private int x;
	private int y;
	
	private DBHelper dbHelper;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.editdevicescreen);  
       
        
    	final DBHelper dbHelper = DBHelper.getInstance(this);
        
    	devices = dbHelper.getDevices();
    	
		final Bundle extras = getIntent().getExtras();
    	installedId = extras.getInt("installedId");

    	this.roomId = extras.getInt("roomId");
    	this.level = extras.getInt("level");
    	this.buildingId = extras.getInt("buildingId");
    	
    	currentWallIndex = extras.getInt("currentWallIndex");
    	
        this.x = extras.getInt("x");
        this.y = extras.getInt("y");
    	
    	if(installedId == -1) {
    		
        	installed = new Installed();
        	installed.setX(x);
        	installed.setY(y);
    	}else {
    		
    		installed = dbHelper.getInstalledByInstalledId(installedId);
    	}

    	
    	
    	//installedDevice = dbHelper.getInstalledByInstalledId(installedId);
    	
    	final Button buttonSave = (Button) findViewById(R.id.buttonSave);
    	buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
			public void onClick(View v) {
            	open();
            	Room room = dbHelper.getRoomByRoomId(roomId);
            	Wall wall = room.getWallArray().get(currentWallIndex);
            	
            	dbHelper.saveInstalledDevice(installed, wall.getId());
            }
        });
        
        final Button buttonDelete = (Button) findViewById(R.id.buttonDelete);
        buttonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
			public void onClick(View v) {
            	open();
            	Room room = dbHelper.getRoomByRoomId(roomId);
            	Wall wall = room.getWallArray().get(currentWallIndex);
            	
            	dbHelper.deleteInstalledDeviceByInstalledId(installedId);
            }
        });
        

        
        deviceTypeArray = new String[devices.size()];
        
        for(int i = 0; i < deviceTypeArray.length; i ++) {
        	deviceTypeArray[i] = devices.get(i).getName();
        }
        
        
        final Spinner devicesInputsSpinner = (Spinner) findViewById(R.id.inputs);
        final Spinner devicesOutputsSpinner = (Spinner) findViewById(R.id.outputs);

        
        final Spinner devicesSpinner = (Spinner) findViewById(R.id.devicetype);
        ArrayAdapter adapterDevicesSpinner = new ArrayAdapter(this,
        		android.R.layout.simple_spinner_item, deviceTypeArray);
        devicesSpinner.setAdapter(adapterDevicesSpinner);
        devicesSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent,
			        View view, int pos, long id) {
				
				deviceInputsArray = new String[devices.get(pos).getInputs().size()];
				inputs = devices.get(pos).getInputs();
				
				deviceOutputsArray = new String[devices.get(pos).getOutputs().size()];
				outputs = devices.get(pos).getOutputs();
				
				installed.setDevice(devices.get(pos));
				
				for(int i = 0; i < deviceInputsArray.length; i ++) {
					deviceInputsArray[i] = inputs.get(i).getUnit().getName();
		        }
				
				for(int i = 0; i < deviceOutputsArray.length; i ++) {
					deviceOutputsArray[i] = outputs.get(i).getUnit().getName();
		        }
				
		        devicesInputsSpinner.setAdapter(new ArrayAdapter(context,
		        		android.R.layout.simple_spinner_item, deviceInputsArray));
		        devicesOutputsSpinner.setAdapter(new ArrayAdapter(context,
		        		android.R.layout.simple_spinner_item, deviceOutputsArray));
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
        	
        });
        
        if(installedId != -1) {
        for(int i = 0; i < devices.size(); i++) {
        	if(installed.getDevice().getId() == devices.get(i).getId()) {
        		devicesSpinner.setSelection(i);
        		
				deviceInputsArray = new String[devices.get(i).getInputs().size()];
				inputs = devices.get(i).getInputs();
				
				deviceOutputsArray = new String[devices.get(i).getOutputs().size()];
				outputs = devices.get(i).getOutputs();
				
				installed.setDevice(devices.get(i));
				
				for(int j = 0; j < deviceInputsArray.length; j ++) {
					deviceInputsArray[j] = inputs.get(j).getUnit().getName();
		        }
				
				for(int j = 0; j < deviceOutputsArray.length; j ++) {
					deviceOutputsArray[j] = outputs.get(j).getUnit().getName();
		        }
				
		        devicesInputsSpinner.setAdapter(new ArrayAdapter(context,
		        		android.R.layout.simple_spinner_item, deviceInputsArray));
		        devicesOutputsSpinner.setAdapter(new ArrayAdapter(context,
		        		android.R.layout.simple_spinner_item, deviceOutputsArray));
        	}
        }
        }

        
        
        devicesInputsSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent,
			        View view, int pos, long id) {
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
        	
        });
        
        devicesOutputsSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent,
			        View view, int pos, long id) {
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
        	
        });
        
    }
    
    public void open() {
    	Intent intent = new Intent(this,AddDeviceScreen.class);
    	intent.putExtra("roomId", roomId);
    	intent.putExtra("level", level);
    	intent.putExtra("buildingId", buildingId);
		intent.putExtra("currentWallIndex", currentWallIndex);
    	startActivity(intent);
    }

}
